找传奇、传世资源到传世资源站!

基于Android studio 的课程表增删查改功能(基于sqlite)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

Android studio 用SQLite实现课程表的增删查改功能,:实现详情浏览、编辑、及新增功能。课程编号可以实现正则检查(编号规则XX00000,XX表示两位大写的字母,00000表示五位十进制数字),并保证编号的唯一性。
from clipboard
from clipboardpackage com.example.kcb;import android.app.Activity;import android.content.Intent;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.Toolbar;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.LinearLayout;import android.widget.RelativeLayout;import android.widget.TextView;import android.widget.Toast;import java.util.ArrayList;public class MainActivity extends AppCompatActivity { //星期几 private RelativeLayout day; //SQLite Helper类 private DatabaseHelper databaseHelper = new DatabaseHelper (this, "database.db", null, 1); int currentCoursesNumber = 0; int maxCoursesNumber = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //工具条 Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); //从数据库读取数据 loadData(); } //从数据库加载数据 private void loadData() { ArrayList<Course> coursesList = new ArrayList<>(); //课程列表 SQLiteDatabase sqLiteDatabase = databaseHelper.getWritableDatabase(); Cursor cursor = sqLiteDatabase.rawQuery("select * from courses", null); if (cursor.moveToFirst()) { do { coursesList.add(new Course( cursor.getString(cursor.getColumnIndex("course_name")), cursor.getString(cursor.getColumnIndex("teacher")), cursor.getString(cursor.getColumnIndex("class_room")), cursor.getInt(cursor.getColumnIndex("day")), cursor.getInt(cursor.getColumnIndex("class_start")), cursor.getInt(cursor.getColumnIndex("class_end")))); } while(cursor.moveToNext()); } cursor.close(); //使用从数据库读取出来的课程信息来加载课程表视图 for (Course course : coursesList) { createLeftView(course); createItemCourseView(course); } } //保存数据到数据库 private void saveData(Course course) { SQLiteDatabase sqLiteDatabase = databaseHelper.getWritableDatabase(); sqLiteDatabase.execSQL ("insert into courses(course_name, teacher, class_room, day, class_start, class_end) " "values(?, ?, ?, ?, ?, ?)", new String[] {course.getCourseName(), course.getTeacher(), course.getClassRoom(), course.getDay() "", course.getStart() "", course.getEnd() ""} ); } //创建"第几节数"视图 private void createLeftView(Course course) { int endNumber = course.getEnd(); if (endNumber > maxCoursesNumber) { for (int i = 0; i < endNumber-maxCoursesNumber; i ) { View view = LayoutInflater.from(this).inflate(R.layout.left_view, null); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(110,180); view.setLayoutParams(params); TextView text = view.findViewById(R.id.class_number_text); text.setText(String.valueOf( currentCoursesNumber)); LinearLayout leftViewLayout = findViewById(R.id.left_view_layout); leftViewLayout.addView(view); } maxCoursesNumber = endNumber; } } //创建单个课程视图 private void createItemCourseView(final Course course) { int getDay = course.getDay(); if ((getDay < 1 || getDay > 7) || course.getStart() > course.getEnd()) Toast.makeText(this, "星期几没写对,或课程结束时间比开始时间还早~~", Toast.LENGTH_LONG).show(); else { int dayId = 0; switch (getDay) { case 1: dayId = R.id.monday; break; case 2: dayId = R.id.tuesday; break; case 3: dayId = R.id.wednesday; break; case 4: dayId = R.id.thursday; break; case 5: dayId = R.id.friday; break; case 6: dayId = R.id.saturday; break; case 7: dayId = R.id.weekday; break; } day = findViewById(dayId); int height = 180; final View v = LayoutInflater.from(this).inflate(R.layout.course_card, null); //加载单个课程布局 v.setY(height * (course.getStart()-1)); //设置开始高度,即第几节课开始 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams (ViewGroup.LayoutParams.MATCH_PARENT,(course.getEnd()-course.getStart() 1)*height - 8); //设置布局高度,即跨多少节课 v.setLayoutParams(params); TextView text = v.findViewById(R.id.text_view); text.setText(course.getCourseName() "\n" course.getTeacher() "\n" course.getClassRoom()); //显示课程名 day.addView(v); //长按删除课程 v.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { v.setVisibility(View.GONE);//先隐藏 day.removeView(v);//再移除课程视图 SQLiteDatabase sqLiteDatabase = databaseHelper.getWritableDatabase(); sqLiteDatabase.execSQL("delete from courses where course_name = ?", new String[] {course.getCourseName()}); return true; } }); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.toolbar, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.add_courses: Intent intent = new Intent(MainActivity.this, AddCourseActivity.class); startActivityForResult(intent, 0); break; case R.id.menu_about: Intent intent1 = new Intent(this, AboutActivity.class); startActivity(intent1); break; } return true; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 0 && resultCode == Activity.RESULT_OK && data != null) { Course course = (Course) data.getSerializableExtra("course"); //创建课程表左边视图(节数) createLeftView(course); //创建课程表视图 createItemCourseView(course); //存储数据到数据库 saveData(course); } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复